package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.phoenixcoin.R;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.AppWithdrawActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AppWithdrawActivity extends BaseActivity<AppWithdrawActivityPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.first_tv_show)
    TextView firstTvShow;
    @BindView(R.id.money_edit)
    EditText moneyEdit;
    @BindView(R.id.first_coin_show)
    TextView firstCoinShow;
    @BindView(R.id.first_ll)
    LinearLayout firstLl;
    @BindView(R.id.first_line_show)
    View firstLineShow;
    @BindView(R.id.second_tv_show)
    TextView secondTvShow;
    @BindView(R.id.num_edit)
    EditText numEdit;
    @BindView(R.id.second_coin_show)
    TextView secondCoinShow;
    @BindView(R.id.second_ll)
    LinearLayout secondLl;
    @BindView(R.id.second_line_show)
    View secondLineShow;
    @BindView(R.id.three_tv_show)
    TextView threeTvShow;
    @BindView(R.id.withdraw_edit)
    EditText withdrawEdit;
    @BindView(R.id.three_coin_show)
    TextView threeCoinShow;
    @BindView(R.id.three_ll)
    LinearLayout threeLl;
    @BindView(R.id.three_line_show)
    View threeLineShow;
    @BindView(R.id.four_tv_show)
    TextView fourTvShow;
    @BindView(R.id.tpwd_edit)
    EditText tpwdEdit;
    @BindView(R.id.four_line_show)
    View fourLineShow;
    @BindView(R.id.app_exit_login)
    Button appExitLogin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private String useFund;
    private double exchangeRate;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_withdraw;
    }

    @Override
    public AppWithdrawActivityPresenter getPresenter() {
        return new AppWithdrawActivityPresenter();
    }

    public static void start(Context context, String useFund, String exchangeRate) {
        Intent intent = new Intent(context, AppWithdrawActivity.class);
        intent.putExtra("useFund", useFund);
        intent.putExtra("exchangeRate", exchangeRate);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        useFund = getIntent().getStringExtra("useFund");
        exchangeRate = Double.valueOf(getIntent().getStringExtra("exchangeRate"));
        moneyEdit.setText(useFund);
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        tvTitle.setText("提现");
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText("提现记录");
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        ClickUtil.click(rightTv, () -> {
            AppRecordActivity.start(this, true);
        });
        numEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    withdrawEdit.setText(NumberUtil.keepMax2(Double.valueOf(s.toString()) * exchangeRate));
                } else {
                    withdrawEdit.setText("");
                }
            }
        });
        ClickUtil.click(appExitLogin, () -> {
            String editNum = numEdit.getText().toString().trim();
            String editMoney = withdrawEdit.getText().toString().trim();
            String editTpwd = tpwdEdit.getText().toString();
            if (TextUtils.isEmpty(editNum)) {
                ToastUtil.showShort("请输入数量");
                return;
            }
            if (TextUtils.isEmpty(editMoney)) {
                ToastUtil.showShort("请输入金额");
                return;
            }
            if (TextUtils.isEmpty(editTpwd)) {
                ToastUtil.showShort("请输入交易密码");
                return;
            }
            if (!CheckUtil.isPayPwd(editTpwd)) {
                ToastUtil.showShort("交易密码格式错误");
                return;
            }
            mPresenter.withDraw(String.valueOf(userData.getId()), "1", editNum, editMoney, "2", editTpwd);
        });
    }

    public void successWithdraw() {
        AppRecordActivity.start(this, true);
    }
}
